﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using System.Data.SqlClient;
using OneDrive.Model;
using Maticsoft.DBUtility;

namespace OneDrive.DAO
{
    public partial class CommunityInfoDAO
    {
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("CommunityID", "tbl_CommunityInfo");
        }

        public static bool Clear()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_CommunityInfo ");

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int iCommunityID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbl_CommunityInfo");
            strSql.Append(" where CommunityID=@CommunityID");
            SqlParameter[] parameters = {
                    new SqlParameter("@CommunityID", SqlDbType.Int)
            };
            parameters[0].Value = iCommunityID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CommunityInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbl_CommunityInfo(");
            strSql.Append("CommunityID,Name,BuiltDate,Developer,Porterty,PortertyFee,Area,Block,Street)");
            strSql.Append(" values (");
            strSql.Append("@CommunityID,@Name,@BuiltDate,@Developer,@Porterty,@PortertyFee,@Area,@Block,@Street)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@CommunityID", SqlDbType.Int),
                    new SqlParameter("@Name", SqlDbType.NVarChar,50),
                    new SqlParameter("@BuiltDate", SqlDbType.NVarChar,20),
                    new SqlParameter("@Developer", SqlDbType.NVarChar,50),
                    new SqlParameter("@Porterty", SqlDbType.NVarChar,20),
                    new SqlParameter("@PortertyFee", SqlDbType.Float),
                    new SqlParameter("@Area", SqlDbType.NVarChar,10),
                    new SqlParameter("@Block", SqlDbType.NVarChar,10),
                    new SqlParameter("@Street", SqlDbType.NVarChar,10)};
            parameters[0].Value = model.CommunityID;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.BuiltDate;
            parameters[4].Value = model.Developer;
            parameters[5].Value = model.Porterty;
            parameters[6].Value = model.PortertyFee;
            parameters[7].Value = model.Area;
            parameters[8].Value = model.Block;
            parameters[9].Value = model.Street;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CommunityInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbl_CommunityInfo set ");
            strSql.Append("CommunityID=@CommunityID,");
            strSql.Append("Name=@Name,");
            strSql.Append("BuiltDate=@BuiltDate,");
            strSql.Append("Developer=@Developer,");
            strSql.Append("Porterty=@Porterty,");
            strSql.Append("PortertyFee=@PortertyFee,");
            strSql.Append("Area=@Area,");
            strSql.Append("Block=@Block,");
            strSql.Append("Street=@Street,");

            SqlParameter[] parameters = {
                    new SqlParameter("@CommunityID", SqlDbType.Int),
                    new SqlParameter("@Name", SqlDbType.NVarChar,50),
                    new SqlParameter("@BuiltDate", SqlDbType.NVarChar,20),
                    new SqlParameter("@Developer", SqlDbType.NVarChar,50),
                    new SqlParameter("@Porterty", SqlDbType.NVarChar,20),
                    new SqlParameter("@PortertyFee", SqlDbType.Float),
                    new SqlParameter("@Area", SqlDbType.NVarChar,10),
                    new SqlParameter("@Block", SqlDbType.NVarChar,10),
                    new SqlParameter("@Street", SqlDbType.NVarChar,10)};

            parameters[1].Value = model.CommunityID;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.BuiltDate;
            parameters[4].Value = model.Developer;
            parameters[5].Value = model.Porterty;
            parameters[6].Value = model.PortertyFee;
            parameters[7].Value = model.Area;
            parameters[8].Value = model.Block;
            parameters[9].Value = model.Street;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int iCommunityID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_CommunityInfo ");
            strSql.Append(" where CommunityID=@CommunityID");
            SqlParameter[] parameters = {
                    new SqlParameter("@CommunityID", SqlDbType.Int)
            };
            parameters[0].Value = iCommunityID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string strCommunityIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_CommunityInfo ");
            strSql.Append(" where CommunityID in (" + strCommunityIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CommunityInfo GetModel(int iCommunityID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 CommunityID,Name,BuiltDate,Developer,Porterty,PortertyFee,Area,Block,Street from tbl_CommunityInfo ");
            strSql.Append(" where CommunityID=@CommunityID");
            SqlParameter[] parameters = {
                    new SqlParameter("@CommunityID", SqlDbType.Int)
            };
            parameters[0].Value = iCommunityID;

            CommunityInfo model = new CommunityInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CommunityInfo DataRowToModel(DataRow row)
        {
            CommunityInfo model = new CommunityInfo();
            if (row != null)
            {
                if (row["CommunityID"] != null && row["CommunityID"].ToString() != "")
                {
                    model.CommunityID = int.Parse(row["CommunityID"].ToString());
                }
                if (row["AgentID"] != null)
                {
                    model.Name = row["AgentID"].ToString();
                }
                if (row["BuiltDate"] != null)
                {
                    model.BuiltDate = row["BuiltDate"].ToString();
                }
                if (row["Developer"] != null && row["Developer"].ToString() != "")
                {
                    model.Developer = row["Developer"].ToString();
                }
                if (row["Porterty"] != null)
                {
                    model.Porterty = row["Porterty"].ToString();
                }
                if (row["PortertyFee"] != null && row["PortertyFee"].ToString() != "")
                {
                    model.PortertyFee = float.Parse(row["PortertyFee"].ToString());
                }
                if (row["Area"] != null && row["Area"].ToString() != "")
                {
                    model.Area = row["Area"].ToString();
                }
                if (row["Block"] != null)
                {
                    model.Block = row["Block"].ToString();
                }
                if (row["Street"] != null)
                {
                    model.Street = row["Street"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CommunityID,Name,BuiltDate,Developer,Porterty,PortertyFee,Area,Block,Street ");
            strSql.Append(" FROM tbl_CommunityInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" CommunityID,Name,BuiltDate,Developer,Porterty,PortertyFee,Area,Block,Street ");
            strSql.Append(" FROM tbl_CommunityInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbl_CommunityInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.CID desc");
            }
            strSql.Append(")AS Row, T.*  from Course T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CommunityInfo> DataTableToList(DataTable dt)
        {
            List<CommunityInfo> modelList = new List<CommunityInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                CommunityInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
        #endregion  BasicMethod

    }
}
